//
//  LoginViewController.m
//  SinaKanDian
//
//  Created by lina on 11-11-1.
//  Copyright (c) 2011年 Sina. All rights reserved.
//

#import "ModifySecretViewController.h"
#import "DataSource.h"
#import "wxAppDelegate.h"

@interface ModifySecretViewController ()

- (void) initTableView;
- (void) initNavigationBar;


//- (UITableViewCell*) tableViewCellWithReuseIdentifier:(NSString *)identifier;

@end

@implementation ModifySecretViewController

@synthesize tipView = _tipView;




#pragma mark - View lifecycle
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationBar];
    [self initTableView];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_texture.png"]];
}

- (void)viewDidUnload
{
	_loginTableView.dataSource = nil;
    [_tipView release];_tipView = nil;
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return NO;
}

- (void)dealloc
{
	_loginTableView.dataSource = nil;
    [_loginTableView release];
    [_oldSecretTextField release];
    [_newSecretTextField release];
      [_repetNewSecretTextField release];
    [_tipView hide];
    [super dealloc];
}

#pragma mark - tableview source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSString *identifier = @"UITableViewCell";
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    CGRect rect = CGRectMake(10, 10, 300, 40);
    UITextField * textField;

    if(indexPath.section == 0)
    {
        _oldSecretTextField = [[UITextField alloc] initWithFrame:rect];
        textField = _oldSecretTextField;
        textField.secureTextEntry = NO;
        textField.placeholder = @"请填写您的真是姓名,方便朋友找到你";
    }
    else if(indexPath.section == 1)
    {
        _newSecretTextField = [[UITextField alloc] initWithFrame:rect];
        textField = _newSecretTextField;
        textField.secureTextEntry = YES; 
        textField.placeholder = @"你的手机号码";

    }
    else {
        _repetNewSecretTextField = [[UITextField alloc] initWithFrame:rect];
        textField = _repetNewSecretTextField;
        textField.secureTextEntry = YES; 
        textField.placeholder = @"你的手机号码";
        
    }

    textField.font = [UIFont systemFontOfSize:14];
    textField.clearButtonMode = YES;
    textField.backgroundColor = [UIColor clearColor];
    textField.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contentView addSubview:textField];
    
    return cell;

}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString*   string = nil;
    if(section == 0)
    {
        string = @"当前密码:";
    }
    else if(section == 1)
    {
        string = @"新密码:";
    }
    else if(section == 2)
    {
        string = @"重复密码:";
    }
    return string;
    
}


#pragma mark - class method


- (void) initTableView
{
    _loginTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStyleGrouped];
    _loginTableView.dataSource = self;
    _loginTableView.rowHeight = 40;
    _loginTableView.scrollEnabled = NO;
   _loginTableView.backgroundColor = [UIColor clearColor];
    _loginTableView.sectionHeaderHeight = 20;
    _loginTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_loginTableView];
}

- (void) initNavigationBar
{
    // title
    UILabel* tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
    [tempLabel setText:@"修改密码"];
    [tempLabel setBackgroundColor:[UIColor clearColor]];
    [tempLabel setTextAlignment:UITextAlignmentCenter];
    [tempLabel setTextColor:[UIColor colorWithRed:111/255.0 green:117/255.0 blue:81/255.0 alpha:1.0]];
    [tempLabel setFont:[UIFont boldSystemFontOfSize:20]];
    self.navigationItem.titleView = tempLabel;
    [tempLabel release];
    tempLabel = nil;
    
    
    UIImage  *backImage  = [UIImage imageNamed:@"btn_back_normal.png"];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 60, 30);
	[leftButton setBackgroundImage:backImage forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"btn_back_press.png"] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(cancelButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem*  leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    //[leftButton release];     
    //leftButton     = nil;
    self.navigationItem.leftBarButtonItem = leftButtonItem;    
    [leftButtonItem release];
    leftButtonItem = nil;
    
    
    UIImage  *registImage = [UIImage imageNamed:@"btn_finish_normal.png"];
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 50, 30);
    
	[rightButton setBackgroundImage:registImage forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"btn_finish_press.png"] forState:UIControlStateHighlighted];
    [rightButton addTarget:self action:@selector(finishButtonPress) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*  rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightButtonItem;    
    [rightButtonItem release];
    rightButtonItem = nil;

}


#pragma 
#pragma mark  user   button  press
- (void) cancelButtonPress
{
    [_tipView hide];
    [self.navigationController popViewControllerAnimated:YES];


}
- (void) finishButtonPress
{
    NSString* oldpsword = [[DataSource shareInstance] md5_hexDigest:_oldSecretTextField.text];
    NSString* newpsword = [[DataSource shareInstance] md5_hexDigest:_newSecretTextField.text];
    NSDictionary* info = [[NSDictionary alloc] initWithObjectsAndKeys:oldpsword/*@"martin.szj@gmail.com"*/,@"old_password",newpsword/*@"martin.szj@gmail.com"*/,@"new_password",nil];
    CoreMsg* msg = [[CoreMsg alloc] init];
    msg.data = SECRETMODIFY;
    msg.ui_delegate = self;
    
    [[NetDataSource shareInstance]postTo:@"http://www.wxlvtu.com/?a=do_update_password" postInfo:info userInfo:msg];
}

#pragma 
#pragma mark -- textfield delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [_oldSecretTextField resignFirstResponder];
    [_newSecretTextField resignFirstResponder];
    [_repetNewSecretTextField resignFirstResponder];
    _loginTableView.contentOffset = CGPointMake(0, 0);

    return YES;

}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
        if(textField == _newSecretTextField)
        {
            _loginTableView.contentOffset = CGPointMake(0, 40);
        }
    else if(textField == _repetNewSecretTextField)
    {
        _loginTableView.contentOffset = CGPointMake(0, 90);
    }
    return YES; 

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [[DataSource shareInstance] saveUserStatusToKeyChain:@"" passwd:@""];
        [[wxAppDelegate getAppDelegate] popAllViewController];
        [[wxAppDelegate getAppDelegate] launchFirstView];
    }
}

#pragma -mark DataSourceDelegate

- (void)onDataSourceSucCallback:(CoreMsg *)coreMsg withData:(id)data
{
    if ([coreMsg.data isEqual: SECRETMODIFY]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码修改成功，请重新登录。" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 2001;
        [alert show];
        [alert release];
    }
}

- (void)onDataSourceFailCallback:(CoreMsg *)coreMsg withData:(id)data
{
    if ([coreMsg.data isEqual: SECRETMODIFY]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:data delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alert.tag = 2001;
        [alert show];
        [alert release];
    }
}

@end

